package love.unknown.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Demo02 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.通过驱动管理器获取连接对象
        Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.108.129:3306/fruitdb?characterEncoding=utf-8"
                , "root", "123456");
        //3.编写SQL语句
        //id,fname,price,fcount,remark
        String sql = "insert into t_fruit values(0,?,?,?,?)";
        //4.创建预处理命令对象
        PreparedStatement ps = conn.prepareStatement(sql);
        //5.填充参数
        ps.setString(1, "榴莲");
        ps.setInt(2, 15);
        ps.setInt(3, 100);
        ps.setString(4, "榴莲是一种神奇的水果");
        //6.执行更新,返回影响行数
        int count = ps.executeUpdate();
        System.out.println(count > 0 ? "添加成功!" : "添加失败!");
        //7.释放资源
        ps.close();
        conn.close();

    }
}
